﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SABnzbdNET.Dialogs;
using SABnzbdNET.ServiceAgents;
using SABnzbdNET.ViewModels;

namespace SABnzbdNET.Views
{
    /// <summary>
    /// Interaction logic for AddTask.xaml
    /// </summary>
    public partial class AddTask
    {
        private string fileName;
        private string safeFileName;
        private ServerAgent agent;
        
        public AddTask()
        {
            InitializeComponent();
            AddTaskViewModel vm = new AddTaskViewModel();
            DataContext = vm;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            agent = new ServerAgent();
            cmbCategoriesCollecion.Collection = ((AddTaskViewModel)DataContext).CurrentServerInfo.Categories;
        }
		
		private void upload_Click(object sender, RoutedEventArgs e)
		{
            var dlg = new Microsoft.Win32.OpenFileDialog { DefaultExt = ".nzb", Filter = "Binary file (.nzb)|*.nzb" };

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
            	fileName = dlg.FileName;
            	safeFileName = dlg.SafeFileName;
            	
                if (!String.IsNullOrEmpty(safeFileName))
                {
                	txtFilePath.Text = fileName;
                }
            }
		}
		
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
        	if (!String.IsNullOrEmpty(safeFileName) || !String.IsNullOrEmpty(txtNewzbinId.Text))
            	StartUpload();
            else
            {
                MessageBox.Show("No .nzb file has been selected for upload and no Newzbin ID has been entered." 
            	                + Environment.NewLine +"Please try again.", "Error in upload", MessageBoxButton.OK);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Windows[1].Close();
        }
		
        private void StartUpload()
        {
            string category = drpCategories.SelectedItem is string ? 
                (string)drpCategories.SelectedItem : ((ComboBoxItem)drpCategories.SelectedItem).Tag.ToString();
            string script = ((ComboBoxItem)drpScript.SelectedItem).Tag.ToString();
            string priority = ((ComboBoxItem)drpPriority.SelectedItem).Tag.ToString();

            Mouse.OverrideCursor = Cursors.AppStarting;
            agent.UploadToServer(fileName, safeFileName, txtNewzbinId.Text, category, script, priority, 
                () => { Mouse.OverrideCursor = null; ((Main)Application.Current.MainWindow).UpdateServerStatus(); });
            Application.Current.Windows[1].Close();
        }
    }
}
